const { formatDate } = require('../common/utils');
const { MEMBER_BALANCE_LOG_TYPE_VALUE, MEMBER_BALANCE_LOG_TYPE_PREFIX_VALUE,
    MEMBER_INTEGRAL_LOG_TYPE_VALUE } = require('../constants/member');
const LOG_ITEM_DATE_FORMAT = 'YYYY.MM.DD HH:mm';

/**
 * 处理余额变动记录
 * @param {Array} list 余额变动记录
 */
const dealBalanceLogList = (list) => {
    return list.map(item => {
        return {
            name: MEMBER_BALANCE_LOG_TYPE_VALUE[item.bal_type],
            date: formatDate(item.created_at, LOG_ITEM_DATE_FORMAT),
            isAdd: MEMBER_BALANCE_LOG_TYPE_PREFIX_VALUE[item.bal_type] === '+',
            value: Math.abs(item.amount),
        };
    });
};

/**
 * 处理积分变动记录
 * @param {Array} list 积分变动记录
 */
const dealIntegralLogList = (list) => {
    return list.map(item => {
        return {
            name: MEMBER_INTEGRAL_LOG_TYPE_VALUE[item.int_type],
            date: formatDate(item.created_at, LOG_ITEM_DATE_FORMAT),
            isAdd: item.num > 0,
            value: Math.abs(item.num),
        };
    });
};

module.exports = {
    dealBalanceLogList,
    dealIntegralLogList,
};